package com.xike.micromemberserver.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xike.entiy.ResultBean;
import com.xike.micromemberserver.entity.BooksEntity;
import com.xike.micromemberserver.entity.ChapterEntity;
import com.xike.micromemberserver.entity.PageInfoMation;
import com.xike.micromemberserver.service.IBooksService;
import com.xike.micromemberserver.service.IChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hongbin
 * @date 2021-04-28 19:59
 * Description：
 */
@Api("章节管理")
@RestController
@RequestMapping("/member/chapter")
public class ChapterController {
    @Resource
    private IChapterService chapterService;
    @Resource
    private IBooksService booksService;

    @ApiOperation("通过图书id查所有章节")
    @PostMapping("/selectAll")
    public ResultBean selectAll(@RequestBody PageInfoMation pageInfo){
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getLimit());
        List<ChapterEntity> booksEntities = chapterService.selectAll(pageInfo.getBookId());
        PageInfo<ChapterEntity> books = new PageInfo<>(booksEntities);
        return ResultBean.builder().code(200).msg("OK").data(books).build();
    }
    @ApiOperation("通过图书id查最新章节")
    @PostMapping("/selectNew")
    public ResultBean selectNew(@RequestBody BooksEntity booksEntity){

        List<ChapterEntity> chapterEntities = chapterService.selectAll(booksEntity.getId());
        if(chapterEntities.size()>0){

            return ResultBean.builder().code(200).msg("OK").data(chapterEntities.get(chapterEntities.size()-1)).build();
        }
        else {
            return ResultBean.builder().code(200).msg("OK").data(ChapterEntity.builder().chapterNum(0).title("作者暂未发布").build()).build();

        }
    }

    @ApiOperation("通过图书id查特定章节")
    @PostMapping("/selectOneByNum")
    public ResultBean selectOneByNum(@RequestBody ChapterEntity currentChapterEntity){
        ChapterEntity chapterEntity = chapterService.selectOneByNum(currentChapterEntity);
        return ResultBean.builder().code(200).msg("OK").data(chapterEntity).build();
    }

    @ApiOperation("图书新增章节")
    @PostMapping("/insert")
    public ResultBean insert(@RequestBody ChapterEntity chapterEntitie){
        //新增章节
        chapterService.insert(chapterEntitie);
        //更改书籍更新时间
        return ResultBean.builder().code(200).msg("OK").build();
    }
    @ApiOperation("图书修改章节")
    @PostMapping("/update")
    public ResultBean update(@RequestBody ChapterEntity chapterEntitie){
        chapterService.update(chapterEntitie);
        return ResultBean.builder().code(200).msg("OK").build();
    }
    @ApiOperation("删除章节")
    @PostMapping("/delete")
    public ResultBean update(@RequestParam("id") int id){
        chapterService.delete(id);
        return ResultBean.builder().code(200).msg("OK").build();
    }

}
